package com.ff.neuropp.service;



import com.ff.neuropp.domain.CareerType;
import com.ff.neuropp.domain.Mentor;
import com.ff.neuropp.repository.MentorRepository;
import com.ff.neuropp.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class MentorService {

    @Resource
    private MentorRepository mentorRepository;


    /**
     * 保存
     * @param mentor
     * @return
     */
    public Mentor saveMentor(Mentor mentor){
        return mentorRepository.save(mentor);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    public Mentor findById(Integer id){
        Optional<Mentor> op = mentorRepository.findById(id);
        return op.isPresent() ? op.get() : null;
    }


    /**
     * 根据id 删除实体
     * @param id
     */
    public void deleteById(Integer id) {
        mentorRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param mentor
     * @return
     */
    public Page<Mentor> findPage(Mentor mentor) {
        Map<String, Object> sqlParams = new HashMap<>();
        if (null == mentor.getPageNo()) {
            mentor.setPageNo(1);
        } else if (mentor.getPageNo() < 1) {
            mentor.setPageNo(1);
        }
        if (null == mentor.getPageSize()) {
            mentor.setPageSize(10);
        }
        sqlParams.put("GT_id",0);
        if(null !=mentor.getSearch() && mentor.getSearch().trim().length()>0){
            sqlParams.put("LIKE_name",mentor.getSearch());
        }
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(mentor.getPageNo()-1,mentor.getPageSize(), direction, sort);
        return mentorRepository.findAll(SpecificationUtil.buildSpecification(sqlParams, Mentor.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<Mentor> findList(Map<String, Object> param) {
        return mentorRepository.findAll(SpecificationUtil.buildSpecification(param, Mentor.class));
    }
}
